package com.jingdianjichi.interview.server.Interceptor;

import com.jingdianjichi.interview.server.context.LoginContextHolder;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * ClassName:LoginInterceptor
 * Package:com.jingdianjichi.subject.application.interceptor
 * Description:
 *
 * @Author fst
 * @Create 2025/1/3 21:15
 * @Version 1.0
 */
public class LoginInterceptor implements HandlerInterceptor
{
    @Override
    public  boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
    {
        String loginId = request.getHeader("loginId");

        if(loginId!=null&&!loginId.equals("")) {
            LoginContextHolder.set("loginId", loginId);
        }
        return true;
    }

    @Override
    public  void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception
    {
        LoginContextHolder.remove();
    }
}
